-- GuardsLeader
-- Created by chengb Jul/29/2016
-- 侍卫队长

-- 侍卫队长对应的冈布奥
local GUARDS_LEADER_PET_ID = 2;

-- 内部函数声明
local talkWithGuardsLeader;

-- 初始化
function initGuardsLeader(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local iconPath = getHeroIconPath(GUARDS_LEADER_PET_ID);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local bonusTimes = BraveVSDragonM.getGuardsLeaderBonusTimes();
            if bonusTimes == 0 then
                -- 还没获得过奖励，触发首次对话
                talkWithGuardsLeader(self);
                return;
            end

            -- 沼泽洞窟复活【侍卫队长】后
            if BraveVSDragonM.hasRevivedGuardsLeader() then
                if bonusTimes > 1 then
                    -- 已经领取所有奖励
                    alert(getLocStr("guards_leader_tip"));
                    return;
                end

                AudioM.playFx("ui_open1");
                local function okFunc()
                    DungeonActionM.go("guards_leader_bonus", self.gridData:getPos())
                end
                self:openBuildingView(okFunc, nil, buildingInfo["dialog2"], nil, nil, iconPath);
                return;
            end
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setScale(1.2);
    self.icon:setVisible(true);
    self.item:setPositionY(-10);

    -- 呼吸
    self.needAnimation = true;

    local bonusTimes = BraveVSDragonM.getGuardsLeaderBonusTimes();

    -- 领取过第一次奖励后，玩家只有复活过侍卫队长格子才显示
    if bonusTimes == 1 and not BraveVSDragonM.hasRevivedGuardsLeader() then
        self.icon:setVisible(false);
    end
end

-- 和侍卫队长首次对话
function talkWithGuardsLeader(self)
    local function endCallback()
        DungeonActionM.go("guards_leader_bonus", self.gridData:getPos())
    end

    -- 触发对话
    startDialogueTemplate("guards_leader_talk1", endCallback);
end